﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.UI.UI
{
    public partial class CheckOut : MTUserControl, IViewCheckOut
    {
        #region IViewCheckOut

        bool _displayRId = true;
        Handsets _handset;
        StudentInfo _student;

        public bool IsDisplayRID
        {
            get { return _displayRId; }
        }

        public string EventRemark
        {
            get { return txtRemark.Text; }
        }

        public Handsets Handset
        {
            get 
            {
                _handset.MAC = txtHandsetID.Text;
                return _handset; }
            set
            {
                _handset = value;
                txtHandsetID.Text = _handset.MAC;
                RaisePropertyChanged(_handset.ToString());
            }
        }

        public StudentInfo Student
        {
            get { return _student; }
            set 
            {
                _student = value;
                txtBarcode.Text = _student.Barcode;
                txtStudentNO.Text = _student.StudentNo;
                txtStudentName.Text = _student.StudentName;
                txtIsGraduate.Text = _student.IsGraduate ? "Yes" : "No";

                RaisePropertyChanged(_handset.ToString());
            }
        }

        public event EventHandler BarcodeEntered;
        public event EventHandler StudentNoEntered;
        public event EventHandler CheckOutClicked;
        public event EventHandler StudentNameEntered;

        public void SetError(string propertyName, string errorMessage)
        {
            //Dictionary<string, Control> controlsByName = new Dictionary<string, Control>();
            //controlsByName.Add(StoplightViewProperties.GreenDuration, greenDurationTextBox);
            //controlsByName.Add(StoplightViewProperties.YellowDuration, yellowDurationTextBox);
            //controlsByName.Add(StoplightViewProperties.RedDuration, redDurationTextBox);


            //if (controlsByName.ContainsKey(propertyName))
            //{
            //    errorProvider.SetError(controlsByName[propertyName], errorMessage);
            //}
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        // Event firing helpers
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void RaiseBarcodeEntered()
        {
            EventHandler handlers = BarcodeEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaiseStudentNoEntered()
        {
            EventHandler handlers = StudentNoEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaiseStudentNameEntered()
        {
            EventHandler handlers = StudentNameEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaiseCheckOutClicked()
        {
            EventHandler handlers = CheckOutClicked;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        public CheckOut()
        {
            InitializeComponent();
            this.Load += new EventHandler(CheckOut_Load);
            this.btnConfirm.Click += OnCheckOutClicked;
            this.txtBarcode.KeyDown += OnKeyDown;
        }

        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (sender == txtBarcode)
                {
                    RaiseBarcodeEntered();
                }

                if (sender == txtStudentNO)
                {
                    RaiseStudentNoEntered();
                }
            }
        }

        void CheckOut_Load(object sender, EventArgs e)
        {

        }

        void OnCheckOutClicked(object sender, EventArgs e)
        {
            RaiseCheckOutClicked();
        }
    }
}
